﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.Service.Interface;

namespace Bymed.SpineScan.Service.Validation
{
    public abstract class DataValidatorBase<T> : IDataValidator<T>
    {
        protected Dictionary<string, string> ErrorDictionary = new Dictionary<string, string>();

        /// <summary>
        /// Add a error into current dictionary.
        /// </summary>
        /// <param name="key">The key of the dictionary.</param>
        /// <param name="errorMessage">The error message will be added into the dictionary.</param>
        public virtual void AddError(string key, string errorMessage)
        {
            ErrorDictionary.Add(key, errorMessage);
        }

        /// <summary>
        /// Get a error from current dictionary.
        /// </summary>
        /// <param name="key">The key of the dictionary.</param>
        /// <returns>The matched error message will be return if it exists. Otherwise the result will be null.</returns>
        public string GetError(string key)
        {
            if (ErrorDictionary.ContainsKey(key))
            {
                return ErrorDictionary[key];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Get the IEnumerable collection contains all error keys.
        /// </summary>
        /// <returns>IEnumerable collection contains all error keys.</returns>
        public IEnumerable GetAllErrorKeys()
        {
            return ErrorDictionary.Keys;
        }

        /// <summary>
        /// True means it's valid. False means invalid.
        /// </summary>
        public virtual bool IsValid
        {
            get
            {
                return ErrorDictionary.Count > 0 ? false : true;
            }
        }

        /// <summary>
        /// Validate the provided entity. Then check the IsValid property to see the result.
        /// </summary>
        /// <param name="entity">The provided entity to be validated.</param>
        public abstract void Validate(T entity);
    }
}
